from django import forms
from django.forms.formsets import formset_factory
from django.utils.translation import ugettext_lazy as _

from aio.models import Draw, Player

class PlayerForm(forms.Form):
    name = forms.CharField(required=True)
    email = forms.EmailField(required=True)
    draw_id = forms.CharField(required=False)
    exclusion_ids = forms.CharField(required=False)
    
    def clean_exclusion_ids(self):
        ids = self.cleaned_data['exclusion_ids']
        try:
            ids_list = eval('[%s]' % ids)
        except:
            raise forms.ValidationError(_("It must be a list of numbers separated by comma"))
        else:
            return ids_list
        
PlayerFormset = formset_factory(PlayerForm, extra=7)

class SorteoForm(forms.ModelForm):

    def clean_body(self):
        body = self.cleaned_data['body']
        if '$AMIGO' not in body and '$FRIEND' not in body:
            raise forms.ValidationError(_("$AMIGO keyword must be somewhere in the text"))
        return body
    
    class Meta:
        model = Draw
        fields = ('subject', 'body', )

    
